//
//  MyDIYWidget.swift
//  MyWidgetTestProject
//
//  Created by shuininzuo on 2021/4/6.
//

import SwiftUI
import WidgetKit

struct MyDIYWidgetEntry: TimelineEntry {
    let date: Date
    let models: [MyDIYWidgetModel]
}

struct MyDIYWidgetProvider: TimelineProvider {
    // 默认占位数据
    func placeholder(in context: Context) -> MyDIYWidgetEntry {
        var models: [MyDIYWidgetModel] = []
        let data = [["title": "快捷选车", "urlScheme": "a"],
                    ["title": "销量排行", "urlScheme": "b"],
                    ["title": "厂家直购", "urlScheme": "c"],
                    ["title": "汽车保养", "urlScheme": "a"]]
        
        for item in data {
            var title = ""
            var urlScheme = ""
            if let n = item["title"] {
                title = n
            }
            if let n = item["urlScheme"] {
                urlScheme = n
            }
            let model = MyDIYWidgetModel(title: title, urlScheme: urlScheme)
            models.append(model)
        }
        return MyDIYWidgetEntry.init(date: Date(), models: models)
    }
    // 快照
    func getSnapshot(in context: Context, completion: @escaping (MyDIYWidgetEntry) -> Void) {
        var models: [MyDIYWidgetModel] = []
        let data = [["title": "快捷选车", "urlScheme": "a"],
                    ["title": "销量排行", "urlScheme": "b"],
                    ["title": "厂家直购", "urlScheme": "c"],
                    ["title": "汽车保养", "urlScheme": "a"]]
        
        for item in data {
            var title = ""
            var urlScheme = ""
            if let n = item["title"] {
                title = n
            }
            if let n = item["urlScheme"] {
                urlScheme = n
            }
            let model = MyDIYWidgetModel(title: title, urlScheme: urlScheme)
            models.append(model)
        }
        completion(MyDIYWidgetEntry.init(date: Date(), models: models))
    }
    // 根据时间线刷新，这里可以进行网络请求
    func getTimeline(in context: Context, completion: @escaping (Timeline<MyDIYWidgetEntry>) -> Void) {
        var entries: [MyDIYWidgetEntry] = []
        // 网络请求数据
        MyNetWorkHelper.request { (models) in
            let entry = MyDIYWidgetEntry.init(date: Date(), models: models)
            entries.append(entry)
            let timeline = Timeline(entries: entries, policy: .atEnd)
            completion(timeline)
        }
    }
}

// 用于显示的UI
struct MyDIYWidgetEntryView: View {
    // 接收数据
    var entry: MyDIYWidgetEntry
    // UI展示
    var body: some View {
        GeometryReader{ geo in
            HStack(spacing: 0) {
                ForEach(entry.models, id: \.title) { model in
                    let url = URL(string: model.urlScheme)
                    // 添加跳转链接url
                    Link(destination: url!) {
                        Text(model.title)
                            .frame(width: geo.size.width / CGFloat(entry.models.count), height: geo.size.height)
                            .font(.system(size: 11, weight: .regular, design: .default))
                            .foregroundColor(.black)
                            .lineLimit(1)
                    }
                }
            }
        }
    }
}

struct MyDIYWidget: Widget {
    let kind = "MyDIYWidget"
    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: MyDIYWidgetProvider()) { entry in
            MyDIYWidgetEntryView(entry: entry)
        }
        .configurationDisplayName("自定义小组件")
        .description("输入你想描述的说明文案")
    }
}
